Music Object Extension - Chords

  Contents

About This Extension

Integrating sonification of a Web page's user interface with background music can be a tricky thing. Especially challenging is the job of tying chromatic (pitched) user interface sounds to the specific key of background music. Well-chosen background music can create a strong soundtrack for a Web site that really enhances the user experience, and it would be a shame to spoil such a soundtrack with inappropriately chosen musical notes and chords.

The methods provided in this extension make it easier to apply a musical knowledge of background music to the sonification of user interface elements. With methods such as playChord, one can easily play chords that blend in harmoniously with the background music, simply by specifying a chord name. The extension allows music-savvy Web developers to apply their knowledge of music in a manner that is familiar to them, proving invaluable as an aid in the development of Web-based music instruction applications and intelligent musical "jam along" applications.

What's New in the Latest Version

There is a dedicated version history for this extension, where you will be able to find out about the new features and possible issues with the most recent version. The version history for this extension is independent of the version history for the core Music Object API.

Getting This Extension

To get the "music-object-x-chords.js" JavaScript file that implements this extension, simply right-click on this link (click-and-hold for Mac users). Then, from the menu that pops up, select the 'Save Link As...' or 'Save Target As...' option and save the file as "music-object-x-chords.js" into an appropriate folder of the local copy of your Web site.

In case you have problems saving the file using the above link, the "music-object-x-chords.js" file can also be found inside the documentation's "javascript" folder, along with the "music-object.js" file.

Dependencies

In order to use this extension, the following JavaScript file(s) must first be sourced in...

For more information on using extensions, consult the section About Music Object Extensions.

Reference

 Name   Short Description 
Instance Methods
playChord Plays a musical chord, as specified by a chord name parameter and other parameters.
playNotes Plays a set of any number of musical notes, as specified by a notes array parameter.
Static Methods
Music.getChordNotes Returns an array, representing the musical notes that make up a specified chord.

Instance Methods

 playChord

 
 CATEGORY   Instance Method 
 MODULE   Music Object Extension - Chords 
 Min. Player Version   1.0.0 
 File Types Supported   RMF, MIDI 
 

The playChord method plays a musical chord, as specified by a chord name parameter and other parameters.

SYNTAX    playChord (
    MidiChannel1to16 ,
    BankNumber0to2 ,
    ProgramNumber0to255 ,
    ChordNameSTR ,
    OctaveNumber0to12 ,
    ChordRootNoteNameSTR ,
    Velocity0to127 ,
    NoteDurationMsINT
)

When using this method, as well as the Music.getChordNotes static method, chord names must be expressed using different combinations of the elements listed below. The elements must appear in the chord name in the order in which they are listed and should not be separated by spaces, or the chord name may not be interpreted correctly. All the elements are optional, with the exception of the chord root which is required.

Chord Name Elements (listed in the order in which they should appear)

  1. A|B|C|D|E|F|G
    one of the letters A, B, C, D, E, F, or G, indicating the root of the chord

  2. [#|b]
    an optional accidental (sharp or flat) following the chord root. The character "#" represents sharp (as in "A#" for A-sharp), and the lowercase letter "b" represents flat (as in "Cb" for C-flat).

  3. [m|min|maj]
    an optional "m" or "min" indicating that it is a minor chord, or "maj" indicating that it is a major chord

  4. [dim]
    an optional "dim", indicating that the chord is diminished. When a chord is diminished, the 5th note is diminished (lowered by a half step). No 7th note is added.

  5. [[#|b]7]
    an optional "7" prefixed with an optional accidental (sharp or flat), indicating that a 7th note is present in the chord, and whether the 7th note is sharp, flat, or natural.

  6. [sus[2|4]]
    an optional "sus" followed by a "2" or "4", indicating that the chord is suspended, and which note replaces the 3rd note, with "sus2" indicating that the 2nd note should replace the 3rd note, and "sus4" indicating that the 4th note should replace the 3rd note.

  7. [[#|b]9]
    an optional "9" prefixed with an optional accidental (sharp or flat), indicating that a 9th note is present in the chord, and whether the 9th note is sharp, flat, or natural. When "9" is present, it is implicit that the chord will contain a 7th note, and so a 7th note is added.

  8. [[#|b][11|13]]
    an optional "11" or "13" prefixed with an optional accidental (sharp or flat), indicating that either an 11th note or 13th note is present in the chord, and whether the note is sharp, flat, or natural. When either "11" or "13" is present, it is implicit that the chord will contain a 7th note, and so a 7th note is created if not already present. Also, the 9th note is set and the 5th and 8th notes are removed.

  9. [[#|b]5]
    an optional "5" prefixed with an optional accidental (sharp or flat), indicating that a 5th note is present in the chord, and whether the 5th note is sharp, flat, or natural.

  10. [[#|b]6]
    an optional "6" prefixed with an optional accidental (sharp or flat), indicating that a 6th note is present in the chord, and whether the 6th note is sharp, flat, or natural.

  11. [+]
    an optional "+" character, indicating that the chord should be augmented. If present, the chord's 5th note is augmented (raised by a half step).

To get a better understanding of how these elements can be combined in different ways to create different chords, see the demo for the Chords extension.

IMPORTANT

The playChord method has the following limitations...

SPECIAL VALUES
VARIATIONS
NOTES
 playNotes

 
 CATEGORY   Instance Method 
 MODULE   Music Object Extension - Chords 
 Min. Player Version   1.0.0 
 File Types Supported   RMF, MIDI 
 

The playNotes method plays a set of any number of musical notes, as specified by a notes array parameter.

SYNTAX    playNotes (
    MidiChannel1to16 ,
    BankNumber0to2 ,
    ProgramNumber0to255 ,
    NotesARRAY ,
    Velocity0to127 ,
    NoteDurationMsINT
)

The playNotes method will play all of the notes contained in the NotesARRAY parameter on the specified MIDI channel. All notes will be played using the same instrument, at the same velocity, and for the same duration, as specified by the BankNumber0to2, ProgramNumber0to255, Velocity0to127, and NoteDurationMsINT parameters.

The elements of the notes array, specified by the NotesARRAY parameter, may be either note names or note numbers. All elements that are not of type "string" or "number" will be ignored. This includes elements that have a value of null, and also elements that are of type "boolean". The fact that null-value elements are ignored by this method is particularly important because the array that is returned by the Music.getChordNotes static method will invariably contain some null-value elements.

The playNotes method is useful as a way of playing an arbitrarily large number of notes simultaneously, since there is no restriction on the number of notes that can be specified by the NotesARRAY parameter. The method is also useful for playing notes that don't conform to any chord that could otherwise be played using the playChord method, or interchangeably playing chord note arrays produced by the Music.getChordNotes static method and note arrays that are generated using a custom function or constructed manually.

The playChord and Music.getChordNotes methods take three parameters to fully qualify the notes that make up a chord: ChordNameSTR, OctaveNumber0to12, and ChordRootNoteNameSTR. Because of this, it isn't possible to call these methods with a single parameter that can represent an exact chord. So, if for example you wanted to design code that iterated in some pattern through an array of chords, then the playNotes method would come in very handy.

In the following code example, the function playNextChord will step through the chords in the array named "chords". When it reaches the end of the "chords" array it will being again from the first element.

chordNo = -1;

function playNextChord () {
   chordNo = (chordNo + 1) % chords.length;
   myMusicObject.playNotes (1,1,11,chords [chordNo],127,2000);
}

chords = new Array (
   Music.getChordNotes ('Eb7#9',3,'Eb'),
   'C#3,C#4,E#4,Db5,C#5,Gb5',
   Music.getChordNotes ('A7#11',3,'A'),
   'C3,D#4,F4,G#4,Bb5',
   Music.getChordNotes ('Emaj9',3,'E'),
   new Array ('C#4','F#4','G#4','B4','D#5'),
   new Array (49,63,65,68,72,75),
   Music.getChordNotes ('Abmin11',3,'Ab'),
);

myMusicObject = new Music ();
myMusicObject.stubEmbed ('stub.rmf');

From the listing above, you will notice what may seem like quite a bit of "hanky-panky" going on with the creation of the "chords" array. Along with several elements that are being created using the Music.getChordNotes method, there are also some elements being declared as simple strings, as well as some other elements being created using the new operator. Because the playNotes method can take either a NotesARRAY parameter or a NoteNamesSTR parameter, it doesn't truly matter that all the elements of the "chords" array are not of the same type. When the playNextChord function selects element 0 of the "chords" array, it will be passing an array reference to the playNotes method. But, when the function selects element 1, it will pass the string value "C#3,C#4,E#4,Db5,C#5,Gb5" to the playNotes method.

The playNotes method is, indeed, quite flexible. In the sample code listing below, all the calls to the playNotes method will play exactly the same chord...

myMusicObject.playNotes (1,1,11,'C#3,D#4,F4,G#4,C5',127,2000);
myMusicObject.playNotes (1,1,11,new Array (49,63,65,68,72),127,2000);
myMusicObject.playNotes (1,1,11,new Array ('C#3','D#4','F4','G#4','C5'),127,2000);
myMusicObject.playNotes (1,1,11,new Array (49,'D#4','F4',68,72),127,2000);

myNotesArray = new Array (49,63,65,68,72);
myMusicObject.playNotes (1,1,11,myNotesArray,127,2000);

Now, let's take a look at the calls to the playNotes method above...

  1. In the first call to the playNotes method, the form of the method that takes a NoteNamesSTR parameter is being used in place of the form that takes a NotesARRAY parameter. This string parameter must always contain a comma-separated list of note names. Note numbers may not be used in the comma-separated list with this form of the method.

  2. In the second call to the playNotes method, an array of note numbers is being created using the new operator at the time of the method call - and actually inside the parameter list of the method call. This is a perfectly valid syntax for the JavaScript language.

  3. In the third call to the playNotes method, an array of note names is being created instead of an array of note numbers. Because the elements of the array referenced by the NotesARRAY parameter can be either note names or note numbers, an array containing only note names is just as valid as an array of only note numbers.

  4. In the fourth call to the playNotes method, the array that is being created and passed as a parameter contains a blend of note names and note numbers. Because the elements of the array referenced by the NotesARRAY parameter can be either note names or note numbers, an array with a blend of names and numbers is also perfectly acceptable.

  5. Finally, in the fifth call to the playNotes method, the variable myNotesArray is being passed for the NotesARRAY parameter. In this case, the array of notes has already been created beforehand and has been assigned to the variable myNotesArray.
SPECIAL VALUES
VARIATIONS
NOTES
Static Methods

 Music.getChordNotes

 
 CATEGORY   Static Method 
 MODULE   Music Object Extension - Chords 
 Min. Player Version   n/a 
 

The Music.getChordNotes static method returns an array, representing the musical notes that make up a specified chord.

SYNTAX    Music.getChordNotes ( ChordNameSTR , OctaveNumber0to12 , ChordRootNoteNameSTR )

RETURN VALUE    NoteNumbersARRAY

The array that is returned by the Music.getChordNotes method will contain exactly 14 elements, with element 0 always containing a null value, and the elements 1 through 13 containing either note numbers or null values. Elements 1 through 13 represent the notes that make up a chord. For example, element 1 will represent the chord root note, element 2 will represent the chord's 2nd note, element 3 will represent the chord's 3rd note, and so on. If the chord does not contain a certain note, then the element corresponding to that chord note will be set to a null value. For instance, if a particular chord does not contain a 7th note, then element 7 will be set to null. On the other hand, if the chord does contain a 7th note, then element 7 will contain the MIDI note number of the chord's 7th note.

The following table shows several example chords with a listing of the MIDI note numbers that make up the chords. In the first three columns are the chord name, octave, and root note that determine the notes of the chord, and in the remaining 13 columns are the values of elements 1 through 13 of the array that will be returned by the Music.getChordNotes method.

Chord Octave Root root 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th
Eb7#9 3 Eb 51 null 55 null 58 null 61 null 66 null null null null
A7#11 3 A 57 null 61 null null null 67 null 71 null 75 null null
Emaj9 3 E 52 null 56 null 59 null 63 null 66 null null null null
Dbsus9 4 Db 61 null null 66 68 null 71 null 75 null null null null
Abmin11 2 Ab 56 null 59 null null null null null 70 null 73 null null
Emaj7#11 4 E 64 null 68 null null null 75 null 78 null 82 null null
Bb7#5#9 3 Bb 58 null 62 null 66 null 68 null null null null null null

The playChord method takes three parameters to fully qualify the notes that make up a chord: ChordNameSTR, OctaveNumber0to12, and ChordRootNoteNameSTR. Because of this, it isn't possible to call this method with a single parameter that can represent an exact chord. So, if for example you wanted to design code to play a randomly selected chord from an array of chords, then you would be better off using the Music.getChordNotes method in conjunction with the playNotes method instead of the playChord method.

In the following code example, the function playRandomChord will randomly select a chord from the array named "chords"...

function playRandomChord () {
   var chordNo = Math.floor (Math.random () * chords.length);
   myMusicObject.playNotes (1,1,11,chords [chordNo],127,2000);
}

chords = new Array (
   Music.getChordNotes ('Eb7#9',3,'Eb'),
   Music.getChordNotes ('A7#11',3,'A'),
   Music.getChordNotes ('Emaj9',3,'E'),
   Music.getChordNotes ('Dbsus9',4,'Db'),
   Music.getChordNotes ('Abmin11',3,'Ab'),
   Music.getChordNotes ('Emaj7#11',4,'E'),
   Music.getChordNotes ('Bb7#5#9',3,'Bb'),
   Music.getChordNotes ('Eb7#9',3,'Eb'),
   Music.getChordNotes ('A7#11',3,'A')
);

myMusicObject = new Music ();
myMusicObject.stubEmbed ('stub.rmf');

In this example, the array of chords is created using the new constructor and is assigned to the variable "chords". Don't mind the fact that the statement is broken up across multiple lines - this is a perfectly acceptable thing to do in JavaScript, provided that you end each line on a logical token and you don't ever have any unterminated string literals. Each element of the array "chords" is, itself, an array containing the MIDI note numbers of the notes that make up a chord. The array for each element is created using a call to the Music.getChordNotes method. Now, when the playRandomChord function selects a chord, it will pass an element of the array "chords" by reference to the playNotes method, and the playNotes method will in turn play the notes of the chord.

The array that is returned by the Music.getChordNotes method is likely to contain some null-value elements. If, for the purposes of your own code, you needed a compacted array of notes containing no null-value elements, then you could write a custom function as follows...

function compactChord (chordName,chordOctave,chordRoot) {
   var
      chordNotes = Music.getChordNotes (chordName,chordOctave,chordRoot),
      compactNotes = new Array ()
   ;
   for (var noteNo = 0; noteNo < chordNotes.length; noteNo++) {
      if (chordNotes [noteNo] != null) {
         compactNotes [compactNotes.length] = chordNotes [noteNo];
      }
   }
   return compactNotes;
}

For detailed guidelines on the chord name syntax that should be used for this method, consult the reference for the playChord method.

SPECIAL VALUES
VARIATIONS
NOTES